import {
  getAddress,
  getCategories,
  getShopInformation
} from '../network'

import {
  ADD_ADDRESS,
  ADD_CATEGORIES,
  ADD_INFORMATION,
  ADD_RESULT,
  ADD_TOKEN
} from './mutations_type'

export default {
  // 获取地理位置
  async getAddressAction({commit}) {
    let result = await getAddress(29.72454, 113.90039)

    !!(result.code === 0) && commit(ADD_ADDRESS, result.data) 
  },
  // 获取商品分类
  async getCategoriesAction({commit}, fn) {
    let result = await getCategories()

    if(!!(result.code === 0)) {
      commit(ADD_CATEGORIES, result.data)
      typeof fn === 'function' && fn()
    }
     
    // && 
  },
  // 获取商店分类
  async getInformationAction({commit}) {
    let result = await getShopInformation(40.10038, 116.36867)

    !!(result.code === 0) && commit(ADD_INFORMATION, result.data)
  },
  getResultAction({commit}, user) {
    commit(ADD_TOKEN, user.token)
    // 将token存入到本地localStroage
    localStorage.setItem('token_id', user.token)
    delete user.token
    commit(ADD_RESULT, user)
  },
}